# @encoding=utf-8
# @Author: QX
# @File: day0052touchaction_move_to.py
'''
1.进入读书屋
2.单击书城
3.依次前三本书加入书架
4.使用tap方式书架
5.常按第一本书，等待3秒
6.接着将第一本书的挪至第三本书的位置,并等待3秒后再弹起
7.单击完成
'''

from time import sleep
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction

dis = {
  "platformName": "Android",
  "platformVersion": "9",
  "appPackage": "com.zhao.myreader",
  "appActivity": ".ui.home.MainActivity",
  "deviceName": "emulator-5554",
  "automationName": "UIAutomator2",
  "noReset": False
}

driver = webdriver.Remote("http://127.0.0.1:4723", dis)
driver.implicitly_wait(30)


action = TouchAction(driver)

# 单击书城
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("书城")').click()

# 依次将前三本书加入书架
books = driver.find_elements(AppiumBy.ID, 'com.zhao.myreader:id/iv_book_img')
for book in books[:3]:
    sleep(1)
    book.click()
    driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("加入书架")').click()
    driver.find_elements(AppiumBy.CLASS_NAME, 'android.widget.ImageView')[0].click()
    sleep(1)
sleep(4)

# 单击书架
action.tap(driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("书架")')).perform()
sleep(3)

# 长按第一本书并移动到第三本书位置
eles = driver.find_elements(AppiumBy.ID, 'com.zhao.myreader:id/iv_book_img')
action.long_press(eles[0],duration=2000).move_to(eles[-1]).release().perform()

# 单击完成
#driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("完成")').click()